package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;

import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.terminalversion.TerminalVersionCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.terminalversion.TerminalVersionCreateForm;
import com.bwda.dsrs.filecenter.domain.form.terminalversion.TerminalVersionQueryForm;
import com.bwda.dsrs.filecenter.domain.form.terminalversion.TerminalVersionUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.terminal.TerminalPo;
import com.bwda.dsrs.filecenter.domain.po.terminalversion.TerminalVersionPo;
import com.bwda.dsrs.filecenter.domain.vo.terminalversion.TerminalVersionVo;
import com.bwda.dsrs.filecenter.service.TerminalService;
import com.bwda.dsrs.filecenter.service.TerminalVersionService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 *
 * @author system
 * @date 2019/4/26
 */
@RestController
@RequestMapping("/b/terminalversion")
@Api(value = "/b/terminalversion", tags = "版本升级表")
public class TerminalVersionController extends BaseController {

	@Autowired
	private TerminalVersionService terminalVersionService;

	@Autowired
	private TerminalService terminalService;

	@ApiOperation(value = "查询版本升级表",notes = "查询版本升级表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalVersionVo> query(@ApiParam(value = "ID", required = true)@RequestParam BigDecimal id) throws BusinessServiceException {
		TerminalVersionPo po = terminalVersionService.queryWithValid(id);
		TerminalVersionVo vo = CopyUtil.transfer(po, TerminalVersionVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询版本升级表数量",notes = "查询版本升级表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TerminalVersionQueryForm form) throws BusinessServiceException {
		TerminalVersionCondition condition = this.getConditionByQueryForm(form);
		int count = terminalVersionService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询版本升级表列表",notes = "查询版本升级表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TerminalVersionVo>> queryList(@ModelAttribute@Valid TerminalVersionQueryForm form) throws BusinessServiceException {
		TerminalVersionCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<TerminalVersionPo> poList = terminalVersionService.queryList(condition);
		List<TerminalVersionVo> voList = CopyUtil.transfer(poList, TerminalVersionVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询版本升级表列表(带分页)",notes = "查询版本升级表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TerminalVersionVo>> queryPageList(@ModelAttribute@Valid TerminalVersionQueryForm form) throws BusinessServiceException {
		TerminalVersionCondition condition = this.getConditionByQueryForm(form);
		List<TerminalVersionVo> voList = new ArrayList<>();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		int count = terminalVersionService.queryCount(condition);
		if (count > 0) {
			List<TerminalVersionPo> poList = terminalVersionService.queryList(condition);
			voList = CopyUtil.transfer(poList, TerminalVersionVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}



	@ApiOperation(value = "新增版本升级表",notes = "新增版本升级表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TerminalVersionVo> add(@ModelAttribute@Valid TerminalVersionCreateForm form) throws BusinessServiceException {
		TerminalVersionPo po =	CopyUtil.transfer(form,TerminalVersionPo.class);
		terminalVersionService.insert(po);
		TerminalPo po1 = new TerminalPo();
		po1.setTerminalCode(form.getTerminalNo());
		po1.setTerminalVersion(form.getCurrentTerminalVersion());
		terminalService.update(po1);
		TerminalVersionVo vo = CopyUtil.transfer(po,TerminalVersionVo.class);
		return getSuccessResult(vo);
	}



	@ApiOperation(value = "修改版本升级表",notes = "修改版本升级表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TerminalVersionUpdateForm form) throws BusinessServiceException {
		TerminalVersionPo po = CopyUtil.transfer(form, TerminalVersionPo.class);
		terminalVersionService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除版本升级表",notes = "删除版本升级表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "ID", required = true)@RequestParam BigDecimal id) throws BusinessServiceException {
		terminalVersionService.delete(id);
		return getSuccessResult();
	}

	/**
	 * TerminalVersionQueryForm转换为TerminalVersionCondition
	 *
	 * @param form
	 * @return
	 */
	private TerminalVersionCondition getConditionByQueryForm(TerminalVersionQueryForm form) {
		TerminalVersionCondition condition = CopyUtil.transfer(form, TerminalVersionCondition.class);
		return condition;
	}

}